package net.newsmth.smthapp.android.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import net.newsmth.smthapp.android.R;
import net.newsmth.smthapp.android.helper.BBSThreadHelper;
import net.newsmth.smthapp.android.helper.BBSThreadHelperImpl;
import net.newsmth.smthapp.android.model.Article;
import net.newsmth.smthapp.android.model.ArticleBody;
import net.newsmth.smthapp.android.model.BBSThread;
import net.newsmth.smthapp.android.ui.ArticleListActivity;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by Hatsune Miku on 5/10/14.
 */
public class ArticleShowAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<Article> articles;


    public ArticleShowAdapter(ArticleListActivity context, List<Article> articles) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);

        this.articles = articles;
    }

    public List<Article> getArticles() {
        return articles;
    }

    public void setArticles(List<Article> articles) {
        this.articles = articles;
    }

    @Override
    public Object getItem(int position) {
        return articles.get(position);
    }

    @Override
    public long getItemId(int position) {
        Article item = (Article)getItem(position);
        return item.getId();
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if (view == null) {
            Article item = (Article) getItem(position);

            View articleView = getArticleView(item);
//        TextView textView = new TextView(this.context);
//        textView.setText(item.getSubject());

            return articleView;
        } else {
            return view;
        }
    }

    @Override
    public int getCount() {
        return this.articles.size();
    }


    public View getArticleView(Article article) {
        View articleView  = inflater.inflate(R.layout.view_article_list, null);

        TextView accountNameTextView = (TextView)articleView.findViewById(R.id.account_name_text_view);
        TextView createdTimeTextView = (TextView)articleView.findViewById(R.id.created_time_text_view);
        TextView floorNumberTextView = (TextView)articleView.findViewById(R.id.floor_number_text_view);
        TextView bodyTextView = (TextView)articleView.findViewById(R.id.article_body_text_view);

        accountNameTextView.setText(article.getAuthorId());
        createdTimeTextView.setText(article.getCreatedTime().toString());
        bodyTextView.setText(article.getBody());
//        bodyTextView.setText(article.getBody());
//        bodyTextView.setText(Html.escapeHtml(article.getBody()));

        return articleView;
    }
}
